package 力扣日常刷题.木22二月.第04天210;

import 力扣日常刷题.TreeNode;

/**
 * @author 帅小伙
 * @date 2022/2/10
 * @description
 */
public class Demo36JZ二叉搜索树的第K大节点 {

    int k,ans = 0;
    public int kthLargest(TreeNode root, int k) {
        this.k = k;
        dfs(root);
        return ans;
    }
    void dfs(TreeNode node){
        if(node == null) return ;
        dfs(node.right);
        if(k == 0) return ;
        if(--k == 0) ans = node.val;
        dfs(node.left);
    }
}
